import api from '@/api';

export default {
  namespace: 'shopaccount',

  state: {
    accountInfo: {},
    isEdit: false,
    airports: [],
    targetItems: [], // 已选机场 穿梭框
    airportFormValue: [], // 已选机场 表单
  },

  effects: {
    // 根据国家查询机场
    *getAirportsByCountry({payload}, {call, put}){
        const response = yield call(api.shopInfo.queryAirportsByCountry, payload)
        if (response.code === 200) {
            const arr =response.data && response.data.map(item => {
                return {
                    key: item.id,
                    title: item.airportName,
                    abbrev: item.abbreviation,
                    code: item.iataCode
                }
            });
            yield put({
                type: 'saveAirport',
                payload: arr
            });
        }
    },
    // 根据城市查询机场
    *getAirportsByCity({payload}, {call, put}){
        const response = yield call(api.shopInfo.queryAirportsByCity, payload)
        if (response.code === 200) {
            const arr = response.data && response.data.map(item => {
                return {
                    key: item.id,
                    title: item.airportName,
                    abbrev: item.abbreviation,
                    code: item.iataCode
                }
            });
            yield put({
              type: 'saveAirport',
              payload: arr
            });
        }
    },
    // 根据ids查询机场   待删除
    *getAirportsNameByIds({payload}, {call, put}){
        console.log('payload', payload)
        const response = yield call(api.shopInfo.queryAirportByIds, payload)
        if(response.code === 200){
            const arr = response.data && response.data.map(item => {
                return {
                    key: item.id,
                    title: item.abbreviation
                }
            });
            yield put({
                type: 'saveAirportFormValue',// 
                payload: arr
            });
        }
        // queryAirportByIds
    },
    // 获取账户信息 store targetItems
    *getAccountInfo({payload}, {call, put}){
        const response = yield call(api.shopInfo.queryAccountInfo, payload);   
        console.log('shop info respones', response)
        if (response.code === 200) {
            const Obj = response.data.airportService
            
            const arr =Obj.map(item=>{
                return {
                    key: item.id,
                    title: item.airportName,
                    abbrev: item.abbreviation,
                    code: item.iataCode,
                }
            })
            console.log('selected arr', arr)
            yield put({
                type: 'saveAccountInfo',
                payload: response.data
            });
            
            yield put({
                type: 'saveAirportFormValue',
                payload: [...arr]
            })
            yield put({
                type: 'saveTargetItems',
                payload: [...arr]
            })
        }
    },
    // 修改账户信息
    *modifyAccountInfo({payload}, {call, put}){
        return yield call(api.shopInfo.modifyAccountInfo, payload)
    },
    // 发送短信
    *sendMes({payload}, { call, put}) {
        return yield call(api.shopInfo.sendMes, payload)     
    },
    // 短信验证
    *validateCode({payload}, {call, put}) {      
        return yield call(api.shopInfo.validateCode, payload)
    },
    // 修改手机号
    *modifyMobile({payload}, { call, put}) {
        return yield call(api.shopInfo.modifyMobile, payload)
    },
  
  },

  reducers: {
    saveAirport(state, action){
        return {
            ...state,
            airports:  action.payload
        }
    },
    // 穿梭框change调用 targetItems
    saveTargetItems(state, action){
        return {
            ...state,
            targetItems: action.payload
        }
    },
    // 穿梭框change调用 airportFormValue
    saveAirportFormValue(state, action){
        return {
            ...state,
            airportFormValue: action.payload
        }
    },
    saveAccountInfo(state, action){
        return {
            ...state,
            accountInfo: action.payload
        }
    },
    toggleEdit(state, action){
        return {
          ...state,
          isEdit: !state.isEdit
        }
    },
    toShowInfo(state, action){
        return {
            ...state,
            isEdit: false
          }
    }
  }
};
